<?php

namespace app\mobile\controller;

use app\common\func\allRequest;

use think\Controller;
use think\Session;
use think\Db;
use think\Request;


class Index extends Controller
{

    public function index()
    {
        // 电影票
            $url = "http://v.juhe.cn/wepiao/query?key=b6e2cd3eaab2bb865b58216448f7b3eb";
            $urlreq = new allRequest();
            $msg = $urlreq->curl_request($url);
            $ggg = json_decode($msg,true);
            // dump($ggg['result']['h5url']);die;

        $res =Db::table('sm_h_movie')
            ->alias('shm')
            ->join('sm_h_am sham', 'sham.mid  = shm.id')
            ->join('sm_h_actor sha', 'sha.id = sham.aid')
            ->field('shm.mname,sha.name,shm.id')
            ->select();
        $data = Db::table('sm_h_movie')
            ->limit(3)
            ->order('id','desc')
            ->select();
        $rank = Db::table('sm_h_movie')
            ->limit(10)
            ->order('score','desc')
            ->field('mname,id')
            ->select();
        // var_dump($rank);die;
        $hot = Db::table('sm_h_movie')
            ->where('score','>=','3')
            ->limit(3)
            ->select();
        $user = Db::table('sm_h_user')
            ->field('name')
            ->where('id', Session::get('id'))
            ->find();
        return view('mobile@Index/index', [
            'res' => $res,
            'user'=>$user,
            'data' => $data,
            'hot' => $hot,
            'rank' => $rank,
            'ggg' => $ggg
            
        ]);
    }

    public function show($id)
    {
        $res = Db::table('sm_h_movie')
            ->alias('shm')
            ->join('sm_h_am sham', 'sham.mid  = shm.id')
            ->join('sm_h_actor sha', 'sha.id = sham.aid')
            ->field('shm.mname,sha.name,shm.id')
            ->where('shm.id',$id)
            ->select();
        $data = Db::table('sm_h_movie')->find($id);
        $type = $data['type'];

        $like =  Db::table('sm_h_movie')->where('type',$type)->where('id','not in',$id)->limit(5)->select();
        $rev = Db::table('sm_h_comment')
            ->alias('shc')
            ->join('sm_h_user shu', 'shu.id = shc.uid')
            ->join('sm_h_movie shm', 'shm.id = shc.mid')
            ->field('shc.title,shc.comment,shc.time,shu.name,shu.face')
            ->where('shm.id',$id)
            ->select();
        $people = Db('h_comment')
            ->where('mid',$id)
            ->count('uid');

        if (empty(Session::get('id'))) {
            return view('mobile@Movie/index', [
                'res' => $res,
                'data' => $data,
                'like' => $like,
                'rev' => $rev,
                'people' => $people
            ]);
        }else{
            $user = Session::get();
            $uid = $user['id'];
            $fav =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_favorite shf', 'shf.mid  = shm.id')
                ->join('sm_h_user shu', 'shu.id = shf.uid')
                ->where('shu.id',$uid)
                ->where('shm.id',$id)
                ->field('shm.id')
                ->find();
            $com =Db::table('sm_h_movie')
                ->alias('shm')
                ->join('sm_h_comment shc', 'shc.mid  = shm.id')
                ->join('sm_h_user shu', 'shu.id = shc.uid')
                ->where('shu.id',$uid)
                ->where('shm.id',$id)
                ->field('shm.id')
                ->find();
            // var_dump($people);die;
            return view('mobile@Movie/index', [
                'res' => $res,
                'data' => $data,
                'like' => $like,
                'rev' => $rev,
                'people' => $people,
                'fav' => $fav,
                'com' => $com
            ]);
        }

    }

    public function newmore()
    {
        $data = Db::table('sm_h_movie')
            ->limit(20)
            ->order('id','desc')
            ->select();


        return view('mobile@Newmore/index', [
            'data' => $data,
        ]);
    }

    public function hotmore()
    {
        $hot = Db::table('sm_h_movie')
            ->where('score','>=','3')
            ->select();


        return view('mobile@Hotmore/index', [
            'hot' => $hot,
        ]);
    }

    public function type($id)
    {
        if ($id == 1) {
            $data = Db::table('sm_h_movie')->where('sm_h_movie.country','中国')
                ->whereor('sm_h_movie.country','台湾')
                ->whereor('sm_h_movie.country','香港')
                ->select();
            return view('mobile@Fenlei/index', [
                'data' => $data,
                'title' =>'华语电影'
            ]);
        }else if ($id == 2) {
            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','美国')
                ->whereor('sm_h_movie.country','英国')
                ->whereor('sm_h_movie.country','法国')
                ->whereor('sm_h_movie.country','德国')
                ->select();
            return view('mobile@Fenlei/index', [
                'data' => $data,
                'title' =>'欧美电影'
            ]);
        }else if ($id == 3) {
            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','韩国')
                ->select();
            return view('mobile@Fenlei/index', [
                'data' => $data,
                'title' =>'韩国电影'

            ]);
        }else if ($id == 4) {
            $data = Db::table('sm_h_movie')
                ->where('sm_h_movie.country','日本')
                ->select();
            return view('mobile@Fenlei/index', [
                'data' => $data,
                'title' =>'日本电影'
            ]);
        }

    }

    public function search()
    {
        $where = [];
        $keyword  = isset($_GET['keyword'])?$_GET['keyword'] : '';
        // var_dump($keyword);die;
        if($keyword){
            $where['mname'] = ['like', '%'.$keyword.'%'];
         
         // var_dump($where);die;
         $list = Db::name('h_movie')->where($where)->order('id DESC')->paginate(5);
         // dump($list->all());die;
         $page = $list->render();
         if ($list->all()) {
         return view('mobile@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'list' => $list,
                'page' => $page
            ]);
         }else{
             return view('mobile@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'res' => '没有找到关于 ['.$keyword.'] 的电影，换个搜索词试试吧。',
            ]);
         }
        }else{
            return view('mobile@Index/search',[
                'title' => '搜索关键字:'.$keyword,
                'res' => '加个搜索词搜索一下吧。',
            ]);
        }


    }

    public function favorite($id)
{
    $user = Session::get();
    $uid = $user['id'];
    $s = [
        'uid' => $uid,
        'mid' => $id
    ];

    $result = Db('h_favorite')->insert($s);
    if ($result > 0) {
        $info['status'] = true;
    } else {
        $info['status'] = false;
    }
    return json($info);
}

public function delfav($id)
{
    $user = Session::get();
    $uid = $user['id'];
    $result = db('h_favorite')->where('mid',$id)->where('uid',$uid)->delete();
    if ($result > 0) {
        $info['status'] = true;
    } else {
        $info['status'] = false;
    }
    return json($info);

}

public function callapi()
    {
        $url = "http://v.juhe.cn/wepiao/query";
        $urlreq = new allRequest();
        $msg = $urlreq->curl_request($url);
        dump($msg);exit;
        dump(json_decode($msg));exit;
    }


}
